package fhp.hlhj.giantfold.fragment

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.Activity
import android.app.ActivityOptions
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import fhp.hlhj.giantfold.MainActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.activity.login.WechatLoginAty
import fhp.hlhj.giantfold.adapter.SelfUnLoginedAdp
import fhp.hlhj.giantfold.adapter.SpendVPAdp
import fhp.hlhj.giantfold.event.RecreateEvent
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import kotlinx.android.synthetic.main.fgm_self_unlogin.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.ArrayList

/**
 * author: FHP
 * data: on 2017\11\18 0018 9:24
 * E-Mail: 103945089@qq.com
 */
class SelfUnLoginFgm : Fragment() {
    private lateinit var rootView :View
    private val STATE_SAVE_IS_HIDDEN = "STATE_SAVE_IS_HIDDEN"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (savedInstanceState != null) {
            val isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_IS_HIDDEN)

            val ft = fragmentManager.beginTransaction()
            if (isSupportHidden) {
                ft.hide(this)
            } else {
                ft.show(this)
            }
            ft.commit()
        }
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putBoolean(STATE_SAVE_IS_HIDDEN, isHidden());
    }
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView = inflater.inflate(R.layout.fgm_self_unlogin,null)
        setPager()
        initListener()
        return rootView
    }
    @SuppressLint("RestrictedApi")
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    private fun initListener() {
        rootView.btLogin.setOnClickListener {
            val intent =Intent(activity, WechatLoginAty::class.java)
            activity.startActivityFromFragment(this,intent,1,ActivityOptions.makeSceneTransitionAnimation(activity,rootView.viewPager,"img").toBundle())
        }
    }
    private fun setPager() {
        var datas = ArrayList<String>()
        datas.add("1")
        datas.add("1")
        datas.add("1")
        datas.add("1")
        rootView.viewPager.adapter=SelfUnLoginedAdp(context,datas)
        MyUtils.setIndicator(datas,context,rootView.indectorLo,rootView.viewPager)

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        LogUtil.log("---------------------------来了来了$resultCode")

        if (resultCode!=Activity.RESULT_OK)return
        when(requestCode){
            1->{
                LogUtil.log("---------------------------来了来了")
                val ft = fragmentManager.beginTransaction()
                ft.hide(this)
                ft.commit()
                val mainActivity = activity as MainActivity
                mainActivity.change(2)
            }
        }
    }
}